// formPackage/pages/agencyModule/applyCityAgency/applyCityAgency.js
import { applyCityAgency, queryCityAgencyDetail } from "../../../../service/agency_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    formData: {
      name: "",
      phone: "",
      companyName: "",
      agentCity: "",
      agentCityCode: "",
      provinceCode: "",
      provinceName: "",
    },
    showSuccessPop: false,
    showCityArea: false,
    disabled: false,//是否可以输入
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryCityAgencyDetail();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  //获取代理详情回填:
  queryCityAgencyDetail() {
    wx.showLoading();
    queryCityAgencyDetail().then(res => {
      wx.hideLoading();
      console.log("JJLog:代理回填是:" + JSON.stringify(res));
      if (res.code == 200) {
        if (res.data) {
          this.setData({
            formData: res.data,
            disabled: true
          })
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryCityAgencyDetail", err);
    })
  },

  singleSelectPop(e) {
    if (this.data.disabled) {
      return;
    }
    let type = e.currentTarget.dataset.type;
    if (type == "city") {
      this.setData({
        showCityArea: true
      })
    }
  },

  //输入绑定
  inputCellChange(e) {
    let detail = e.detail;
    let changeItem = `formData.${detail.key}`;
    this.setData({
      [changeItem]: detail.value
    })
  },



  //确认选择城市
  cityPopConfirm(e) {
    // console.log("JJLog:c:" + JSON.stringify(e));
    let result = e.detail.result;
    // [{ "code": "510000", "name": "四川省" }, { "code": "510400", "name": "攀枝花市" }]
    this.setData({
      'formData.agentCity': result[1].name,
      'formData.agentCityCode': result[1].code,
      'formData.provinceName': result[0].name,
      'formData.provinceCode': result[0].code,
      showCityArea: false
    })


  },

  cityPopHidden() {
    this.setData({
      showCityArea: false
    })
  },

  //确认提交申请:
  submitApplyAction() {
    let data = this.data.formData;
    if (!data.name) {
      wx.showToast({
        title: '请输入您的姓名',
        icon: "none"
      })
      return;
    }
    if (!data.phone) {
      wx.showToast({
        title: '请输入您的电话',
        icon: "none"
      })
      return;
    }
    if (!data.agentCity) {
      wx.showToast({
        title: '请选择您要代理的城市',
        icon: "none"
      })
      return;
    }
    console.log("JJLog:提交数据是;" + JSON.stringify(data));
    debounce(this.applyCityAgency);
  },

  //提交请求;
  applyCityAgency() {
    wx.showLoading();
    applyCityAgency(this.data.formData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          showSuccessPop: true
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "提交失败",
        icon: "none"
      })
      console.error("JJLog:applyCityAgency", err);
    })
  },


  //邀请海报
  shareAction() {
    wx.navigateTo({
      url: '/formPackage/pages/platformModule/agencyInvitePage/agencyInvitePage',
    })
  },

  successPopSure() {
    this.setData({
      showSuccessPop: false,
    })
    wx.switchTab({
      url: '/pages/home/home',
    })
  },




})